﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LIK.WakeOnLan.Network;

namespace LIK.WakeOnLan.Forms
{
    public partial class FormNetworkClient : XtraForm
    {
        NetworkClient networkClient = new NetworkClient();

        internal NetworkClient NetworkClient
        {
            get
            {
                this.networkClient.Update(this.textEditIpTitle.Text,
                    this.textEditIpAddress.Text, this.textEditMacAddress.Text, (int)this.spinEditPortN.Value);
                return networkClient;
            }
            set
            {
                this.textEditIpTitle.Text = value.HostName;
                this.textEditIpAddress.Text = value.IpAddress;
                this.textEditMacAddress.Text = value.MacAddress;
                this.spinEditPortN.Value = value.Port;
                this.networkClient = value;
            }
        }

        internal void InEditMode()
        {
            this.simpleButtonAdd.Text = "Update";
        }

        public FormNetworkClient()
        {
            InitializeComponent();
        }

        private void simpleButtonAdd_Click(object sender, EventArgs e)
        {
            this.dxErrorProvider.ClearErrors();
            this.ValidateChildren(ValidationConstraints.Enabled);
            if (this.dxErrorProvider.HasErrors)
            {
                this.DialogResult = DialogResult.None;
                return;
            }
            this.DialogResult =DialogResult.OK;
        }


        private void textEditMacAddress_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textEditMacAddress.Text))
            {
                this.dxErrorProvider.SetError(textEditMacAddress, "Enter valid MAC address");
            }
        }

        private void textEditIpAddress_Validating(object sender, CancelEventArgs e)
        {
            string strIpAddress = textEditIpAddress.Text;
            if (string.IsNullOrEmpty(strIpAddress))
            {
                goto err;
            }
            string[] ipData = strIpAddress.Split('.');
            if (ipData.Length != 4)
            {
                goto err;
            }
            foreach (string subIpAddress in ipData)
            {
                if (string.IsNullOrEmpty(subIpAddress))
                {
                    goto err;
                }
            }
            return;
            err:
            this.dxErrorProvider.SetError(textEditIpAddress, "Enter valid IP address");
        }

        private void spinEditPortN_Validating(object sender, CancelEventArgs e)
        {
            if (spinEditPortN.Value <= 0)
            {
                this.dxErrorProvider.SetError(spinEditPortN, "Enter valid port number");
            }
        }

        private void btnScanNetwork_Click(object sender, EventArgs e)
        {
            using (var frmNetworkDevices = new FormNetworkScanner())
            {
                if(frmNetworkDevices.ShowDialog(this) == DialogResult.OK)
                {
                    var nClient = frmNetworkDevices.NetworkClient;
                    this.textEditMacAddress.Text = nClient.MacAddress;
                    this.textEditIpAddress.Text = nClient.IpAddress;
                    if (!string.IsNullOrEmpty(nClient.HostName))
                    {
                        this.textEditIpTitle.Text = nClient.HostName;
                    }
                }
            }
        }






    }
}
